package cn.sise.edu.crm.web.controller;

import cn.sise.edu.crm.domain.Employee;
import cn.sise.edu.crm.domain.JsonResult;
import cn.sise.edu.crm.domain.Customer;
import cn.sise.edu.crm.domain.SystemDictionaryItem;
import cn.sise.edu.crm.query.CustomerQueryObject;
import cn.sise.edu.crm.service.ICustomerService;
import cn.sise.edu.crm.service.IEmployeeService;
import cn.sise.edu.crm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by chen on 2019/11/3.
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    //潜在列表
    @RequestMapping("/potentialList")
    @RequiresPermissions(value = {"潜在客户页面", "customer:potentialList"}, logical = Logical.OR)
    public String potentialList(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        if (!(subject.hasRole("admin") || subject.hasRole("Market_Manager"))) {
            qo.setSellerId(employee.getId());//查询所有负责的客户
        }
        //只能查潜在客户
        qo.setStatus(Customer.STATUS_POTENTIAL);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        //查询所有销售人员
        List<Employee> sellers = employeeService.queryByRoleSn(new String[]{"Market_Manager", "Market"});
        model.addAttribute("sellers", sellers);

        List<SystemDictionaryItem> jobs = systemDictionaryItemService.queryBySystemDictionarySn("job");
        model.addAttribute("jobs", jobs);
        List<SystemDictionaryItem> sources = systemDictionaryItemService.queryBySystemDictionarySn("source");
        model.addAttribute("sources", sources);
        List<SystemDictionaryItem> com = systemDictionaryItemService.queryBySystemDictionarySn("com");
        model.addAttribute("com", com);
        return "customer/potentialList";
    }
    //客户列表
    @RequestMapping("/list")
    @RequiresPermissions(value = {"客户页面", "customer:list"}, logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        if (!(subject.hasRole("admin") || subject.hasRole("Market_Manager"))) {
            qo.setSellerId(employee.getId());//查询所有负责的客户
        }
        qo.setStatus(-1);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);


        List<Employee> sellers = employeeService.queryByRoleSn(new String[]{"Market_Manager", "Market"});
        model.addAttribute("sellers", sellers);

        List<SystemDictionaryItem> jobs = systemDictionaryItemService.queryBySystemDictionarySn("job");
        model.addAttribute("jobs", jobs);
        List<SystemDictionaryItem> sources = systemDictionaryItemService.queryBySystemDictionarySn("source");
        model.addAttribute("sources", sources);
        return "customer/list";
    }

    //客户池列表
    @RequestMapping("/poolList")
    //@RequiresPermissions(value = {"客户页面", "customer:poolList"}, logical = Logical.OR)
    public String poolList(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        if (!(subject.hasRole("admin") || subject.hasRole("Market_Manager"))) {
            qo.setSellerId(employee.getId());//查询所有负责的客户
        }
        qo.setStatus(Customer.STATUS_POOL);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);


        List<Employee> sellers = employeeService.queryByRoleSn(new String[]{"Market_Manager", "Market"});
        model.addAttribute("sellers", sellers);

        List<SystemDictionaryItem> jobs = systemDictionaryItemService.queryBySystemDictionarySn("job");
        model.addAttribute("jobs", jobs);
        List<SystemDictionaryItem> sources = systemDictionaryItemService.queryBySystemDictionarySn("source");
        model.addAttribute("sources", sources);
        return "customer/poolList";
    }
    //失败客户列表
    @RequestMapping("/failList")
    //@RequiresPermissions(value = {"客户页面", "customer:failList"}, logical = Logical.OR)
    public String failList(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        if (!(subject.hasRole("admin") || subject.hasRole("Market_Manager"))) {
            qo.setSellerId(employee.getId());//查询所有负责的客户
        }
        qo.setStatus(Customer.STATUS_FAILURE);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        List<Employee> sellers = employeeService.queryByRoleSn(new String[]{"Market_Manager", "Market"});
        model.addAttribute("sellers", sellers);

        List<SystemDictionaryItem> jobs = systemDictionaryItemService.queryBySystemDictionarySn("job");
        model.addAttribute("jobs", jobs);
        List<SystemDictionaryItem> sources = systemDictionaryItemService.queryBySystemDictionarySn("source");
        model.addAttribute("sources", sources);
        return "customer/failList";
    }
    //正式客户列表
    @RequestMapping("/formalList")
    //@RequiresPermissions(value = {"客户页面", "customer:formalList"}, logical = Logical.OR)
    public String formalList(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        if (!(subject.hasRole("admin") || subject.hasRole("Market_Manager"))) {
            qo.setSellerId(employee.getId());//查询所有负责的客户
        }
        qo.setStatus(Customer.STATUS_FORMAL);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        List<Employee> sellers = employeeService.queryByRoleSn(new String[]{"Market_Manager", "Market"});
        model.addAttribute("sellers", sellers);

        List<SystemDictionaryItem> jobs = systemDictionaryItemService.queryBySystemDictionarySn("job");
        model.addAttribute("jobs", jobs);
        List<SystemDictionaryItem> sources = systemDictionaryItemService.queryBySystemDictionarySn("source");
        model.addAttribute("sources", sources);
        return "customer/formalList";
    }
    //流失客户列表
    @RequestMapping("/lostList")
    //@RequiresPermissions(value = {"客户页面", "customer:lostList"}, logical = Logical.OR)
    public String lostList(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        if (!(subject.hasRole("admin") || subject.hasRole("Market_Manager"))) {
            qo.setSellerId(employee.getId());//查询所有负责的客户
        }
        qo.setStatus(Customer.STATUS_LOST);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        List<Employee> sellers = employeeService.queryByRoleSn(new String[]{"Market_Manager", "Market"});
        model.addAttribute("sellers", sellers);

        List<SystemDictionaryItem> jobs = systemDictionaryItemService.queryBySystemDictionarySn("job");
        model.addAttribute("jobs", jobs);
        List<SystemDictionaryItem> sources = systemDictionaryItemService.queryBySystemDictionarySn("source");
        model.addAttribute("sources", sources);
        List<SystemDictionaryItem> com = systemDictionaryItemService.queryBySystemDictionarySn("com");
        model.addAttribute("com", com);

        return "customer/lostList";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"潜在客户增加或更新", "customer:saveOrUpdate"}, logical = Logical.OR)
    public JsonResult saveOrUpdate(Customer customer) {
        try {
            if (customer.getId() != null) {

                customerService.update(customer);
            } else {
                customerService.insert(customer);
            }
        } catch (Exception e) {
            return new JsonResult("保存或者更新失败", false);
        }

        return new JsonResult("保存或者更新成功", true);
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Customer customer) {
        try {
           customerService.updateByStatus(customer);
        } catch (Exception e) {
            return new JsonResult("修改失败", false);
        }

        return new JsonResult("修改成功", true);

    }
}
